package com.esri.core.symbol;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.webkit.URLUtil;
import com.esri.core.internal.util.d;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;

/* loaded from: classes.dex */
public final class PictureFillSymbol extends FillSymbol {
    public static final String TYPE = "esriPFS";
    private static final String c = "url";
    private static final String d = "height";
    private static final String e = "width";
    private static final String f = "xoffset";
    private static final String g = "yoffset";
    private static final String h = "contentType";
    private static final String i = "imageData";
    private static final long serialVersionUID = 1;
    private String j;
    private String k;
    private Drawable l;
    private float m;
    private float n;
    private float o;
    private float p;

    public PictureFillSymbol(PictureFillSymbol pictureFillSymbol) throws Exception {
        this.a = pictureFillSymbol.a;
        this.m = pictureFillSymbol.m;
        this.n = pictureFillSymbol.n;
        this.l = pictureFillSymbol.getDrawable();
        if (pictureFillSymbol.b != null) {
            this.b = (LineSymbol) pictureFillSymbol.getOutline().copy();
        } else {
            this.b = null;
        }
    }

    public PictureFillSymbol(Drawable drawable) {
        this.l = drawable;
        this.a = Color.argb(255, 255, 255, 255);
    }

    public PictureFillSymbol(Drawable drawable, SimpleLineSymbol simpleLineSymbol) {
        this(drawable);
        this.b = simpleLineSymbol;
    }

    public PictureFillSymbol(JsonNode jsonNode) throws Exception {
        super(jsonNode);
        a((int) d.a(jsonNode, e, 0.0f));
        b((int) d.a(jsonNode, d, 0.0f));
        setOffsetX((int) d.a(jsonNode, f, 0.0f));
        setOffsetY((int) d.a(jsonNode, g, 0.0f));
        String a = d.a(jsonNode, "url");
        this.k = a;
        this.k = URLUtil.isAboutUrl(a) ? this.k : null;
        this.l = null;
        try {
            byte[] b = d.b(jsonNode, i);
            if (b != null) {
                byte[] decode = Base64.decode(b, 0);
                String str = this.k;
                if (str == null) {
                    str = "PFS" + System.nanoTime();
                }
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode);
                this.l = Drawable.createFromStream(byteArrayInputStream, str);
                byteArrayInputStream.close();
            }
            String str2 = this.k;
            if (str2 != null && this.l == null) {
                try {
                    a(str2);
                } catch (MalformedURLException e2) {
                    if (this.l == null) {
                        throw e2;
                    }
                }
            }
            this.j = d.a(jsonNode, h);
        } catch (Exception e3) {
            e3.printStackTrace();
        }
    }

    public float getWidth() {
        return this.m;
    }

    void a(float f2) {
        this.m = f2;
    }

    public float getHeight() {
        return this.n;
    }

    void b(float f2) {
        this.n = f2;
    }

    public PictureFillSymbol setSize(int i2, int i3) {
        a(i2);
        b(i3);
        return this;
    }

    public float getOffsetX() {
        return this.o;
    }

    public PictureFillSymbol setOffsetX(float f2) {
        this.o = f2;
        return this;
    }

    public float getOffsetY() {
        return this.p;
    }

    public PictureFillSymbol setOffsetY(float f2) {
        this.p = f2;
        return this;
    }

    public Drawable getDrawable() {
        return this.l;
    }

    @Override // com.esri.core.symbol.Symbol
    public String toJson() throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        a.writeStringField("type", TYPE);
        String str = this.k;
        if (str == null || str.length() == 0) {
            Drawable drawable = this.l;
            if (drawable != null) {
                if (drawable instanceof BitmapDrawable) {
                    a(a, (BitmapDrawable) drawable);
                } else {
                    a(a, a());
                }
            }
        } else {
            a.writeStringField(h, this.j);
            a.writeStringField("url", this.k);
        }
        d.a(a, e, this.m);
        d.a(a, d, this.n);
        d.a(a, f, this.o);
        d.a(a, g, this.p);
        super.a(a);
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    private final BitmapDrawable a() {
        if (this.l != null) {
            Bitmap createBitmap = Bitmap.createBitmap((int) getWidth(), (int) getHeight(), Bitmap.Config.ARGB_8888);
            this.l.draw(new Canvas(createBitmap));
            return new BitmapDrawable(createBitmap);
        }
        return null;
    }

    static void a(JsonGenerator jsonGenerator, BitmapDrawable bitmapDrawable) throws IOException, Exception {
        Bitmap bitmap;
        if (bitmapDrawable == null || (bitmap = bitmapDrawable.getBitmap()) == null) {
            return;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 70, byteArrayOutputStream);
        byteArrayOutputStream.flush();
        d.a(jsonGenerator, i, byteArrayOutputStream.toByteArray());
        d.a(jsonGenerator, h, "image/png");
        byteArrayOutputStream.close();
    }

    private void a(String str) throws MalformedURLException, IOException {
        if (str != null) {
            URL url = new URL(str);
            this.l = Drawable.createFromStream(url.openStream(), url.getFile());
        }
    }

    @Override // com.esri.core.symbol.Symbol
    public Symbol copy() throws Exception {
        return new PictureFillSymbol(this);
    }
}
